from PyQt5.QtWidgets import *
from PyQt5 import QtGui
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import pymysql
import interface.teacher.tea_main0 as tea_main0
import io
from PIL.ImageQt import ImageQt,Image
import  base64
import database.admin.setting as setting
import PIL
import interface.teacher.add_course_Window as add_course_Window
import interface.teacher.his_c_Window as his_c_Window
import interface.Main0_Window as Main0_Window
class tea_main0_Window(QMainWindow,tea_main0.Ui_Form):
    def __init__(self,id):
        QMainWindow.__init__(self)
        tea_main0.Ui_Form.__init__(self)
        self.setupUi(self)
        self.setWindowTitle("教师操作界面")
        self.id=id
        self.add_c.clicked.connect(self.load_addc)
        self.view_c.clicked.connect(self.load_viewc)
        self.init()
        self.quit.clicked.connect(self.close)
        self.back.clicked.connect(self.load_pre)
        s0_ = QPixmap("teacher/s0.png")
        self.s0.setPixmap(s0_)
        self.s0.setScaledContents(True)
        s1_ = QPixmap("teacher/s1.png")
        self.s1.setPixmap(s1_)
        self.s1.setScaledContents(True)
        s2_ = QPixmap("teacher/s2.png")
        self.s2.setPixmap(s2_)
        self.s2.setScaledContents(True)
        p = QPalette()
        p_ = QPixmap("s000.jpg")
        p_ = p_.scaled(self.width(), self.height())
        p.setBrush(QPalette.Background, QBrush(p_))
        self.setPalette(p)
    def load_pre(self):
        self.pre=Main0_Window.Main0_Window()
        self.close()
        self.pre.show()
    def load_viewc(self):
        print("load course")
        self.viewc_window=his_c_Window.his_c_window(self.id)
        self.viewc_window.show()
    def load_addc(self):
        self.add_course_window=add_course_Window.add_course_window(self.id)
        self.add_course_window.show()
    def pil2pixmap(self, im):
        if im.mode == "RGB":
            r, g, b = im.split()
            im = Image.merge("RGB", (b, g, r))
        elif im.mode == "RGBA":
            r, g, b, a = im.split()
            im = Image.merge("RGBA", (b, g, r, a))
        elif im.mode == "L":
            im = im.convert("RGBA")
        # Bild in RGBA konvertieren, falls nicht bereits passiert
        im2 = im.convert("RGBA")
        data = im2.tobytes("raw", "RGBA")
        qim = QtGui.QImage(data, im.size[0], im.size[1], QtGui.QImage.Format_ARGB32)
        pixmap = QtGui.QPixmap.fromImage(qim)
        return pixmap
    def init(self):
        self.connect = pymysql.connect(**setting.config)
        self.cursor = self.connect.cursor()
        sql_ = "select * from teachers where id=%s"
        self.cursor.execute(sql_, self.id)
        data = self.cursor.fetchone()
        id_ = self.id
        name_ = data[1]
        sex_ = data[2]
        school_ = data[3]
        job_ = data[4]
        self.name = name_
        self.job = job_
        print(data)
        data1 = base64.b64decode(data[5])
        file_like = io.BytesIO(data1)
        img = PIL.Image.open(file_like)
        # img.show()
        # map_ = QtGui.QPixmap(img)
        # img= PIL.ImageQt.ImageQt(map_)
        # qim = ImageQt(img)
        # map_ = QtGui.QPixmap.fromImage(qim)
        map_ = self.pil2pixmap(img)
        self.pic.setPixmap(map_)
        self.pic.setScaledContents(True)
        self.id_label.setText(id_)
        self.name_label.setText(name_)
        self.job_label.setText(job_)
        self.school_label.setText(school_)
        #self.sex_label.setText(sex_)
if __name__=='__main__':
    app=QApplication(sys.argv)
    md=tea_main0_Window('2018333333')
    md.show()
    sys.exit(app.exec_())